<!--系统管理 --- 组织机构 --- 机构管理 -->
<template>
  <div class="mechanism">
    <div class="mechanism_box">
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left"></div>
        <!-- 右侧 -->
        <div class="print">
          <div class="printTable">
            <el-button type="primary" icon="el-icon-refresh" size="small" @click="getDataList(companyId)"></el-button>
            <el-button v-if="lr_add" type="primary" icon="el-icon-circle-plus-outline" size="small"
              @click="getDetail(fCompanyid)">新增
            </el-button>
          <!-- <el-button type="primary" icon="el-icon-edit" size="small"
              >附加信息</el-button
                  > -->
            <el-button v-if="lr_edit" type="primary" icon="el-icon-edit" size="small" @click="editText(fCompanyid)">编辑
            </el-button>
            <el-button v-if="lr_delete" type="primary" icon="el-icon-delete" size="small"
              @click="rowDelete(fCompanyid)">删除</el-button>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <div class="text_table mechanism_table commonTable" id="mianBody">
        <el-table ref="multipleTable" :data="tableData" style="width: 100%" height="100%" default-expand-all
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" row-key="fCompanyid" v-loading="loading"
          element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)" :row-class-name="tableRowClassName" border stripe
          highlight-current-row @current-change="handleCurrentChange" @row-click="getRowInfo">
          <el-table-column fixed type="index" label="序号" width="50" align="center"></el-table-column>
          <el-table-column prop="fFullname" label="机构名称" style="display: flex">
            <template slot-scope="scope">
              <a href="javascript:;" :title="scope.row.fFullname">{{
                scope.row.fFullname
              }}</a>
            </template>
          </el-table-column>
          <el-table-column prop="fEncode" label="机构编码" width="180" align="center"></el-table-column>
          <el-table-column prop="fShortname" label="机构简称" width="200">
            <template slot-scope="scope">
              <div class="lineBoxhidden">
                <a href="javascript:;" :title="scope.row.fShortname">{{
                  scope.row.fShortname
                }}</a>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="fNature" label="机构性质" width="120" align="center">
          </el-table-column>
          <el-table-column prop="fNote" label="备注" width="500">
            <template slot-scope="scope">
              <div class="lineBoxhidden">
                <a href="javascript:;" :title="scope.row.fNote">{{
                  scope.row.fNote
                }}</a>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--弹框 新增 -->
      <div class="Control-1 mechanism_dialog">
        <el-dialog :close-on-click-modal="false" title="新增" v-dialogDrag :visible.sync="dialogCheckVisible"
          v-loading="loadingCheckDialog" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)">
          <div class="dialogBody common" :style="{ height: dialogHeight }">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
              <div class="tableRow">
                <el-form-item label="机构名称" prop="fFullname" id="flex-able" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fFullname" autocomplete="off" placeholder="最多输入80个文本"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="机构简称" prop="fShortname" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fShortname" autocomplete="off" placeholder="最多输入25个文本"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="省" prop="fProvinceid" :label-width="formLabelWidth">
                  <el-select v-model="ruleForm.fProvinceid" placeholder="请选择" @change="getCityList($event)">
                    <el-option v-for="(v, i) in provinceList" :key="i" :value="v.fAreaname"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="市" prop="fCityid" :label-width="formLabelWidth">
                  <el-select v-model="ruleForm.fCityid" placeholder="请选择" @change="getAreaList($event)">
                    <el-option v-for="(c, num) in cityList" :key="num" :value="c.fAreaname"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="区/县" prop="fCountyid" :label-width="formLabelWidth">
                  <el-select v-model="ruleForm.fCountyid" placeholder="请选择">
                    <el-option v-for="(a, item) in areaList" :key="item" :value="a.fAreaname"></el-option>
                  </el-select>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="机构性质" prop="fNature" :label-width="formLabelWidth">
                  <el-select v-model="ruleForm.fNature" placeholder="请选择">
                    <el-option v-for="(v, i) in fNatureList" :key="i" :label="v.fItemName" :value="v.fItemValue">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="上级机构" prop="fFullnameRow" :label-width="formLabelWidth">
                  <el-select v-model="fFullnameRow" placeholder="请选择" autocomplete="off" disabled>
                    <el-option label="子单位工程" value="子单位工程"></el-option>
                  </el-select>
                </el-form-item>
              </div>

              <div class="tableRow">
                <el-form-item label="负责人" prop="fManager" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fManager" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="fPhone" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fPhone" autocomplete="off"></el-input>
                </el-form-item>
              </div>

              <div class="tableRow">
                <el-form-item label="机构编码" prop="fEncode" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fEncode" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="fOrder" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fOrder" autocomplete="off" placeholder="请输入排序号"></el-input>
                </el-form-item>
              </div>

              <div class="tableRow">
                <el-form-item label="项目地址" prop="fAddress" :label-width="formLabelWidth">
                  <el-input v-model="formData.fAddress" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="第三方机构名称" prop="fPrincipalname" :label-width="formLabelWidth">
                  <el-input v-model="formData.fPrincipalname" autocomplete="off"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="经度" prop="fLon" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fLon" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="纬度" prop="fLat" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fLat" autocomplete="off"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow SelectTime">
                <el-form-item label="开工时间" prop="fStartdates" :label-width="formLabelWidth">
                  <el-date-picker v-model="ruleForm.fStartdates" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="完工时间" prop="fEnddates" :label-width="formLabelWidth">
                  <el-date-picker v-model="ruleForm.fEnddates" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
                  </el-date-picker>
                </el-form-item>
              </div>
              <div class="tableRow" >
                <el-form-item label="合同编号" prop="fHtnumber" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fHtnumber" type="text"  placeholder="请输入内容" >
                  </el-input>
                </el-form-item>
              </div>
              <div class="tableRow" style="margin-top: 10px">
                <el-form-item label="检测单位" prop="fNote" :label-width="formLabelWidth">
                  <el-input v-model="ruleForm.fNote" type="textarea" :rows="4" placeholder="请输入内容" autocomplete="off">
                  </el-input>
                </el-form-item>
              </div>
              <el-form-item class="newlyAdded_btn">
                <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                <el-button type="warning" @click="resetForm('ruleForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-dialog>
      </div>
      <!--弹框 编辑 -->
      <div class="Control-1 mechanism_dialog">
        <el-dialog :close-on-click-modal="false" title="编辑" v-dialogDrag :visible.sync="dialogTableVisible" v-loading="loadingDialog"
          element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)">
          <div class="dialogBody common" :style="{ height: dialogHeight }">
            <el-form :model="formData" :rules="rules" ref="formData">
              <div class="tableRow">
                <el-form-item label="机构名称" prop="fFullname" :label-width="formLabelWidth">
                  <el-input v-model="formData.fFullname" autocomplete="off" placeholder="最多输入80个文本"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="机构简称" prop="fShortname" :label-width="formLabelWidth">
                  <el-input v-model="formData.fShortname" autocomplete="off" placeholder="最多输入25个文本"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="省" prop="fProvinceid" :label-width="formLabelWidth">
                  <el-select v-model="formData.fProvinceid" placeholder="请选择" @change="getCityList($event)">
                    <el-option v-for="(v, i) in provinceList" :key="i" :value="v.fAreaname"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="市" prop="fCityid" :label-width="formLabelWidth">
                  <el-select v-model="formData.fCityid" placeholder="请选择" @change="getAreaList($event)">
                    <el-option v-for="(c, num) in cityList" :key="num" :value="c.fAreaname"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="区/县" prop="fCountyid" :label-width="formLabelWidth">
                  <el-select v-model="formData.fCountyid" placeholder="请选择">
                    <el-option v-for="(a, item) in areaList" :key="item" :value="a.fAreaname"></el-option>
                  </el-select>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="机构性质" prop="fNature" :label-width="formLabelWidth">
                  <el-select v-model="formData.fNature" placeholder="请选择">
                    <el-option v-for="(v, i) in fNatureList" :key="i" :label="v.fItemName" :value="v.fItemValue">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="上级机构" :label-width="formLabelWidth">
                  <el-select v-model="fParentName" placeholder="请选择" autocomplete="off" disabled>
                    <el-option label="子单位工程" value="子单位工程"></el-option>
                  </el-select>
                </el-form-item>
              </div>

              <div class="tableRow">
                <el-form-item label="负责人" prop="fManager" :label-width="formLabelWidth">
                  <el-input v-model="formData.fManager" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="fPhone" :label-width="formLabelWidth">
                  <el-input v-model="formData.fPhone" autocomplete="off"></el-input>
                </el-form-item>
              </div>

              <div class="tableRow">
                <el-form-item label="机构编码" prop="fEncode" :label-width="formLabelWidth">
                  <el-input v-model="formData.fEncode" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="fOrder" :label-width="formLabelWidth">
                  <el-input v-model="formData.fOrder" autocomplete="off" placeholder="请输入排序号"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow">
                <el-form-item label="项目地址" prop="fAddress" :label-width="formLabelWidth">
                  <el-input v-model="formData.fAddress" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="第三方机构名称" prop="fPrincipalname" :label-width="formLabelWidth">
                  <el-input v-model="formData.fPrincipalname" autocomplete="off"></el-input>
                </el-form-item>
              </div>

              <div class="tableRow">
                <el-form-item label="经度" prop="fLon" :label-width="formLabelWidth">
                  <el-input v-model="formData.fLon" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="纬度" prop="fLat" :label-width="formLabelWidth">
                  <el-input v-model="formData.fLat" autocomplete="off"></el-input>
                </el-form-item>
              </div>
              <div class="tableRow SelectTime">
                <el-form-item label="开工时间" prop="fStartdates" :label-width="formLabelWidth">
                  <el-date-picker v-model="formData.fStartdates" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                    autocomplete="off">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="完工时间" prop="fEnddates" :label-width="formLabelWidth">
                  <el-date-picker v-model="formData.fEnddates" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"
                    autocomplete="off">
                  </el-date-picker>
                </el-form-item>
              </div>
              <div class="tableRow" style="margin-top: 10px">
                <el-form-item label="合同编号" prop="fHtnumber" :label-width="formLabelWidth">
                  <el-input v-model="formData.fHtnumber" type="text"  placeholder="请输入内容" >
                  </el-input>
                </el-form-item>
              </div>
              <div class="tableRow" >
                <el-form-item label="检测单位" prop="fNote" :label-width="formLabelWidth">
                  <el-input v-model="formData.fNote" type="textarea" :rows="4" placeholder="请输入内容" autocomplete="off">
                  </el-input>
                </el-form-item>
              </div>
              <el-form-item class="newlyAdded_btn">
                <el-button type="primary" @click="submitForm2('formData')">保存</el-button>
                <el-button type="warning" @click="resetForm2()">关闭</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getBaseCompanyList,
  addBaseCompany,
  getBaseCompanyByFid,
  delBaseCompany,
  Area,
} from "@/api/request.js";

export default {
  computed: {
    fullAddress: function () {
      return (
        this.selected.province +
        "-" +
        this.selected.city +
        "-" +
        this.selected.area
      );
    },
  },
  data() {
    return {
      name: "mechanism",
      lr_add: null,
      lr_edit: null,
      lr_delete: null,

      tableData: [],
      currentRow: null, //点击行变色
      dialogHeight: "",
      loading: false,
      formLabelWidth: "90px",
      fCompanyid: "", //当前行的机构id
      fParentid: "", //父级id
      fNatureList: "", //机构性质树

      // 新增表单
      fFullnameRow: "", //当前行机构全称
      dialogCheckVisible: false, //弹窗的显示隐藏
      loadingCheckDialog: false,
      addId: null, //新增传入id
      ruleForm: {
        fCompanyid: "", //机构id
        fParentid: 0, //父级id
        fEncode: "", //机构编码
        fShortname: "", //机构简称
        fFullname: "", //机构全称
        fNature: "", //机构性质
        fProjectid: "", //项目id
        fManager: "", //机构负责人
        fPhone: "", //机构联系电话
        fLon: "", //经度
        fLat: "", //纬度
        fAddress: "", //项目地址
        fCompanyids: "", //隶属机构id
        fCompanynames: "", //隶属机构名称
        fOrder: "", //排序号
        fPrincipalid: "", //绑定第三方机构id
        fPrincipalname: "", //绑定第三方机构名称
        fProvinceid: "", //省
        fCityid: "", //市
        fCountyid: "", //区
        fStartdates: "", //机构开工时间
        fEnddates: "", //完工时间
        fNote: "", //备注
        userId: "", //当前登录用户id
        userName: "", //当前登录用户
        fHtnumber:"",
      },
      rules: {
        fFullname: [
          { required: true, message: "请输入机构全称", trigger: "blur" },
          { min: 1, max: 80, message: "最多输入80个文本", trigger: "blur" },
        ],
        fShortname: [
          { required: true, message: "请输入机构简称", trigger: "blur" },
          { min: 1, max: 25, message: "最多输入25个文本", trigger: "blur" },
        ],
        fNature: [
          { required: true, message: "请选择机构性质", trigger: "change" },
        ],
        fManager: [
          { required: false, message: "请输入机构负责人", trigger: "blur" },
        ],
        fPhone: [
          { required: false, message: "请输入机构联系电话", trigger: "blur" },
        ],
        fEncode: [
          { required: false, message: "请输入机构编码", trigger: "blur" },
        ],
        fOrder: [{ required: false, message: "请输入排序号", trigger: "blur" }],
        fStartdates: [
          { required: false, message: "请选择开工时间", trigger: "blur" },
        ],
        fPrincipalname: [
          { required: false, message: "请输入第三方机构名称", trigger: "blur" },
        ],
        fEnddates: [
          { required: false, message: "请选择完工时间", trigger: "blur" },
        ],
        fAddress: [
          { required: false, message: "请输入项目地址", trigger: "blur" },
        ],
        fHtnumber: [
          { required: false, message: "请输入项目地址", trigger: "blur" },
        ],
      },
      // 编辑
      loadingDialog: false,
      dialogTableVisible: false,
      formData: {
        fCompanyid: "", //机构id
        fParentid: 0, //父级id
        fEncode: "", //机构编码
        fShortname: "", //机构简称
        fFullname: "", //机构全称
        fNature: "", //机构性质
        fProjectid: "", //项目id
        fManager: "", //机构负责人
        fPhone: "", //机构联系电话
        fLon: "", //经度
        fLat: "", //纬度
        fAddress: "", //项目地址
        fCompanyids: "", //隶属机构id
        fCompanynames: "", //隶属机构名称
        fOrder: "", //排序号
        fPrincipalid: "", //绑定第三方机构id
        fPrincipalname: "", //绑定第三方机构名称
        fProvinceid: "", //省
        fCityid: "", //市
        fCountyid: "", //区
        fStartdates: "", //机构开工时间
        fEnddates: "", //完工时间
        fNote: "", //备注
        userId: "", //当前登录用户id
        userName: "", //当前登录用户
        fHtnumber:"",
      },
      row: null,
      companyTree: "", //所有数据
      fParentName: "", //父级名称
      // 省市区
      fAreaid: "0",
      regionFid: "", //区域父id
      provinceList: [], //省列表
      cityList: [], //市列表
      areaList: [], //区列表
    };
  },
  created() {
    this.fNatureList = JSON.parse(
      window.sessionStorage.getItem("yjSmart-CompanyNature")
    );
  },
  mounted() {
    this.getDataList();
    this.getHeight();
    this.getProvince(this.fAreaid);
    this.btnRole(this.name);
  },

  methods: {
    btnRole(name) {
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          console.log(item);
          btnStr.push(item.fEncode);
        });

        this.lr_add = btnStr.includes("lr_add");
        this.lr_edit = btnStr.includes("lr_edit");
        this.lr_delete = btnStr.includes("lr_delete");
      } else {
        this.lr_add = true;
        this.lr_edit = true;
        this.lr_delete = true;
      }
    },
    getDate(e) {
      console.log(e);
    },
    // 行颜色
    tableRowClassName({ row }) {
      //
      if (
        (row.fParentid == 0 && !row.children) ||
        (row.fParentid == null && !row.children)
      ) {
        return "fristColor";
      }
    },
    // 获取列表数据
    getDataList(id) {
      this.loading = true;
      getBaseCompanyList({
        companyId: id,
      })
        .then((res) => {
          this.companyTree = res.data.baseCompanyList;
          if (res.errcode == 0) {
            var that = this;
            setTimeout(() => {
              that.loading = false;
              that.tableData = that.tranListToTreeData(that.companyTree);
              console.log(111, that.tableData);
              that.$nextTick(() => {
                that.$refs.multipleTable.doLayout();
              });
            }, 200);
          } else {
            this.$message.error(res.msg);
            this.loading = false;
          }
        })
        .catch((error) => {
          this.$message.error(error.msg);
          this.loading = false;
        });
    },

    tranListToTreeData(data) {
      let result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      // 清除已存在的数据  重新赋值
      data.forEach((item) => {
        delete item.children;
      });

      let map = {};
      data.forEach((item) => {
        map[item.fCompanyid] = item;
      });

      data.forEach((item) => {
        let parent = map[item.fParentid];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },

    // 点击当前行
    getRowInfo(e) {
      this.row = e;
      this.fCompanyid = e.fCompanyid;
      this.fParentid = e.fParentid;
      this.fFullnameRow = e.fFullname;
      // 通过父id，找全称
      for (let i = 0; i < this.companyTree.length; i++) {
        if (e.fParentid == this.companyTree[i].fCompanyid) {
          return (this.fParentName = this.companyTree[i].fFullname);
        }
      }
    },
    // 点击当前行变色
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    // 新增
    getDetail(id) {
      this.ruleForm = {
        fCompanyid: "", //机构id
        fParentid: 0, //父级id
        fEncode: "", //机构编码
        fShortname: "", //机构简称
        fFullname: "", //机构全称
        fNature: "", //机构性质
        fProjectid: "", //项目id
        fManager: "", //机构负责人
        fPhone: "", //机构联系电话
        fLon: "", //经度
        fLat: "", //纬度
        fAddress: "", //项目地址
        fCompanyids: "", //隶属机构id
        fCompanynames: "", //隶属机构名称
        fOrder: "", //排序号
        fPrincipalid: "", //绑定第三方机构id
        fPrincipalname: "", //绑定第三方机构名称
        fProvinceid: "", //省
        fCityid: "", //市
        fCountyid: "", //区
        fStartdates: "", //机构开工时间
        fEnddates: "", //完工时间
        fNote: "", //备注
        userId: "", //当前登录用户id
        userName: "", //当前登录用户
      };
      if (id != 0) {
        this.addId = id;
      } else {
        this.addId = 0;
      }
      this.dialogCheckVisible = true;
    },
    // 新增确认
    submitForm() {
      this.loadingCheckDialog = true;
      var that = this;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.fParentid = this.addId;
          addBaseCompany(that.ruleForm)
            .then((res) => {
              if (res.errcode == 0) {
                this.loadingCheckDialog = false;
                this.dialogCheckVisible = false;
                this.getDataList();
                this.$message.success("新增成功");
              } else {
                this.$message.error(res.msg);
                this.loadingCheckDialog = false;
              }
            })
            .catch((error) => {
              this.$message.error(error.msg);
              this.loadingCheckDialog = false;
            });
        } else {
          this.$message.error("请填写必填项");
          this.loadingCheckDialog = false;
        }
      });
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 获取页面容器高度
    getHeight() {
      var h = document.documentElement.clientHeight; //获取页面可见高度
      // var w = document.documentElement.clientWidth; //获取页面可见高度
      this.dialogHeight = h * 0.5 + "px";
    },

    // 编辑
    editText(id) {
      if (id != "") {
        this.dialogTableVisible = true;
        this.loadingDialog = true;
        getBaseCompanyByFid({ fCompanyid: id })
          .then((res) => {
            if (res.errcode == 0) {
              var that = this;
              that.loadingDialog = false;
              that.dialogTableVisible = true;
              that.formData = res.data.lrBaseCompany;
            } else {
              this.$message.error(res.msg);
              this.loadingDialog = false;
            }
          })
          .catch((error) => {
            this.$message.error(error.msg);
            this.loadingDialog = false;
          });
      } else {
        this.$message.error("请先选择需要编辑的机构");
        this.loadingDialog = false;
      }
    },
    //  确认修改：
    submitForm2() {
      var that = this;
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loadingDialog = true;
          this.ruleForm.fParentid = this.addId;
          addBaseCompany(that.formData)
            .then((res) => {
              if (res.errcode == 0) {
                this.dialogTableVisible = false;
                this.loadingDialog = false;
                this.getDataList();
                this.$message.success("修改成功");
              } else {
                this.$message.error(res.msg);
                this.loadingDialog = false;
              }
            })
            .catch((error) => {
              console.log("编辑确认", error.msg);
              this.loadingDialog = false;
            });
        } else {
          this.$message.error("请填写必填项");
          return false;
        }
      });
    },
    // 关闭
    resetForm2() {
      this.dialogTableVisible = false;
    },
    // 删除
    rowDelete(id) {
      if (id != "") {
        this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.loading = true;
            if (this.row.children == undefined) {
              delBaseCompany({ fCompanyid: id }).then((res) => {
                if (res.errcode == 0) {
                  this.loading = false;
                  this.getDataList();
                  this.$message.success("删除成功");
                } else {
                  this.$message.error(res.msg);
                  this.loading = false;
                }
              });
            } else {
              this.$message.error("有下属组织机构不能删除");
              this.loading = false;
            }
          })
          .catch(() => {
            this.$message("取消删除");
            this.loading = false;
          });
      } else {
        return this.$message.error("请先选择需要删除的机构");
      }
    },
    // 省
    getProvince(id) {
      this.loading = true;
      Area({ fAreaid: id })
        .then((res) => {
          this.provinceList = res.data.BaseAreaList;
          if (res.errcode == 0) {
            this.loading = false;
            this.formData.fCityid = "";
            this.formData.fCountyid = "";
          } else {
            this.$message.error(res.msg);
            this.loading = false;
          }
        })
        .catch((error) => {
          this.$message.error(error.msg);
          this.loading = false;
        });
    },
    // 市
    async getCityList(e) {
      let id = "";
      for (let i = 0; i < this.provinceList.length; i++) {
        if (this.provinceList[i].fAreaname == e) {
          let that = this;
          id = that.provinceList[i].fAreaid;
          that.loading = true;
          Area({ fAreaid: id })
            .then((res) => {
              that.cityList = res.data.BaseAreaList;
              if (res.errcode == 0) {
                this.loading = false;
                this.ruleForm.fCityid = "";
                this.ruleForm.fCountyid = "";
                this.formData.fCityid = "";
                this.formData.fCountyid = "";
              } else {
                this.$message.error(res.msg);
                this.loading = false;
              }
            })
            .catch((error) => {
              this.loading = false;
              this.$message.error(error.msg);
            });
        }
      }
    },
    // 区
    async getAreaList(e) {
      let id = "";
      for (let i = 0; i < this.cityList.length; i++) {
        if (this.cityList[i].fAreaname == e) {
          let that = this;
          id = that.cityList[i].fAreaid;
          that.loading = true;
          Area({ fAreaid: id })
            .then((res) => {
              that.areaList = res.data.BaseAreaList;
              if (res.errcode == 0) {
                this.loading = false;
                this.ruleForm.fCountyid = "";
                this.formData.fCountyid = "";
              } else {
                this.$message.error(res.msg);
                this.loading = false;
              }
            })
            .catch((error) => {
              this.$message.error(error.msg);
              this.loading = false;
            });
        }
      }
    },
  },
};
</script>

<style scoped>
.mechanism {
  padding: 5px;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
}

.mechanism_Table {
  width: 100%;
  overflow-x: auto;
}

a {
  text-decoration: none;
  color: #000;
}
</style>